import express from 'express'
import { countSql, detailSql, getResult, listSql, onDuplicByFiled, putSql } from '../../../util/operateSql.js';
import { getAccount } from '../../../middleWare/user/index.js';

var router = express.Router();

const TABLE1 = 'activity_master.a_website_template'
const TABLE2 = 'activity_master.a_website_template_detail'

router.get('/query',async (req,res,next) => {
	const sql = listSql(TABLE1,{...req.query})
	
	const sql2 = countSql(TABLE1,{...req.query})
	const rows = await getResult(sql)
	const countRows = await getResult(sql2)
	res.json(req.app.locals.success({list:rows,total: countRows[0].count}))
})
router.post('/update',getAccount,async (req,res,next) => {
	const {id: templateId,is_apply} = req.body
	const sql = putSql(TABLE1,{is_apply},{id:+templateId,user_id: req.master.id})
	const row = await getResult(sql)
	res.json(req.app.locals.success('修改成功'))
})
router.post('/detail',getAccount,async (req,res,next) => {
	const {id: templateId} = req.body
	if (!templateId) {
		res.json(req.app.locals.error('请选择模版'))
		return
	}
	const sql = detailSql(TABLE2,{website_template_id:+templateId,user_id: req.master.id})
	const row = await getResult(sql)
	res.json(req.app.locals.success(row[0]))
})
router.post('/detail/save',getAccount,async (req,res,next) => {
	const id = req.master.id
	let {company_name,logo,wechat_code,banner_list,category_id,news_cate_id,service_cate_id,phone,website_template_id,advance,dev_process,email,address,rich_text} = req.body
	logo = logo.map(e => e.url)
	banner_list = banner_list.map(e => e.url).join(',')
const sql = onDuplicByFiled(TABLE2,{company_name,phone,logo,category_id,news_cate_id,service_cate_id,wechat_code,banner_list,advance,dev_process,email,address,rich_text,website_template_id,user_id: id})
getResult(sql).then((result) => {
	res.json(req.app.locals.success('success'))
}).catch((err) => {
	console.log('err: ', err);
	
});
})
export default router